package com.jijs.ws.cxfclient;

import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.handler.Handler;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.jijs.ws.cxfclient.handler.LicenseHandler;
import com.jijs.ws.cxfclient.interceptor.LicenseOutInterceptor;
import com.jijs.ws.cxfclient.interceptor.LicenseUserOutInterceptor;

public class CXFClient {
	
	//基于JAX-WS的方式
	public static void test1(){
		IMyService ms = new IMyServiceService().getIMyServicePort();
		System.out.println(ms.sayHello("张三"));
	}
	
	//http://cxf.apache.org/docs/a-simple-jax-ws-service.html
	//使用cxf的方式访问
	public static void test2(){
		JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
		fac.setServiceClass(IMyService.class);
		fac.setAddress("http://localhost:8080/ws?wsdl");
		//可以通过CXF为访问增加相应的Interceptor来处理进去和出来的消息
		fac.getInInterceptors().add(new LoggingInInterceptor());
		fac.getOutInterceptors().add(new LoggingOutInterceptor());
//		List<Handler> list = new ArrayList<Handler>();
//		list.add(new LicenseHandler());
//		fac.setHandlers(list);
		IMyService ms = (IMyService)fac.create();
		System.out.println(ms.sayHello("李四"));
	}
	
	public static void test3(){
		JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
		fac.setServiceClass(IMyService.class);
		fac.setAddress("http://localhost:8080/ws?wsdl");
		//可以通过CXF为访问增加相应的Interceptor来处理进去和出来的消息
		fac.getInInterceptors().add(new LoggingInInterceptor());
		fac.getOutInterceptors().add(new LoggingOutInterceptor());
		
		//自定义Interceptor
		fac.getOutInterceptors().add(new LicenseOutInterceptor());
		IMyService ms = (IMyService)fac.create();
		System.out.println(ms.sayHello("李四"));
	}
	
	public static void test4(){
		JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
		fac.setServiceClass(IMyService.class);
		fac.setAddress("http://localhost:8080/ws?wsdl");
		//可以通过CXF为访问增加相应的Interceptor来处理进去和出来的消息
		fac.getInInterceptors().add(new LoggingInInterceptor());
		fac.getOutInterceptors().add(new LoggingOutInterceptor());
		
		//自定义Interceptor
		fac.getOutInterceptors().add(new LicenseUserOutInterceptor());
		IMyService ms = (IMyService)fac.create();
		System.out.println(ms.sayHello("李四"));
	}

	public static void main(String[] args) {
//		test1();
//		test2();
//		test3();
		test4();
	}
}
